import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {



    }


    public static void main9(String[] args) {
        //打印节点
        //方式①:print
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);

        LinkedList<Integer> list = new LinkedList<>(arrayList);
            System.out.println(list);

            System.out.println("=======");
            //方式②:for循环遍历打印
            for(int i = 0 ; i < list.size() ; i++){
                System.out.print(list.get(i)+ " ");
        }
        System.out.println();
        System.out.println("=============");
        //方式③:for-each
        for(Integer x : list){
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println("==============");

        //方式④:通过迭代器遍历
        //(1)正向打印
        ListIterator<Integer> it = list.listIterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("======================");

        //(2)反向打印
        ListIterator<Integer> it2 = list.listIterator(list.size());
        while(it2.hasPrevious()){
            System.out.print(it2.previous()+ " ");
        }
        System.out.println();
    }






    //==========================================================
    public static void main8(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(1);
        myLinkedList.addLast(2);
        myLinkedList.addLast(3);

        myLinkedList.display();
        System.out.println("=======");
        myLinkedList.clear();
        myLinkedList.display();


    }
    public static void main7(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(111);
        myLinkedList.addLast(222);
        myLinkedList.addLast(111);
        myLinkedList.addLast(111);
        myLinkedList.addLast(333);
        myLinkedList.addLast(111);
        myLinkedList.addLast(999);
        myLinkedList.addLast(111);

        myLinkedList.display();
        System.out.println("=========");
        myLinkedList.removeAllKey(111);
        myLinkedList.display();
    }

    public static void main6(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(11);
        myLinkedList.addLast(22);
        myLinkedList.addLast(33);
        myLinkedList.addLast(44);
        myLinkedList.addIndex(2,1000000000);
        myLinkedList.display();
    }

    public static void main5(String[] args) {
//        LinkedList<Integer> list = new LinkedList<>();

        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(2);
        myLinkedList.addFirst(3);
        myLinkedList.addFirst(4);
        myLinkedList.display();

    }


    public static MySingleList.ListNode mergeTwoLists(MySingleList.ListNode headA, MySingleList.ListNode headB) {

        //实例化静态内部类对象
        //先创建需要用到的新节点newHead和temp。
        MySingleList.ListNode newHead = new MySingleList.ListNode(-1);
        MySingleList.ListNode temp = newHead;

        while(headA != null && headB != null) {
            if (headA.val > headB.val) {
                temp.next = headB;
                headB = headB.next;
                temp = temp.next;
            } else {
                temp.next = headA;
                headA = headA.next;
                temp = temp.next;//原本的temp也需要移动到新链表的下一个节点
            }
        }

        //headA链表还没走完
        if(headA != null){
            temp.next = headA;
        }

        //headB链表还没走完
        if(headB != null){
            temp.next = headB;
        }

        //将合并后的新链表除了这个虚拟头结点的后面所有节点返回出来
        return newHead.next;

    }


    public static void main3(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(23);
        mySingleList.addLast(45);
        mySingleList.addLast(12);
        mySingleList.addLast(34);
        mySingleList.addLast(6);
        mySingleList.display();
        System.out.println();
        // 获取原链表的头结点并调用 partition
        MySingleList.ListNode originalHead = mySingleList.getHead();
        MySingleList.ListNode newHead = MySingleList.partition(originalHead, 30);

        // 更新链表的新头结点并显示结果
        mySingleList.setHead(newHead);
        mySingleList.display();
    }














    public static void main2(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(13);
        mySingleList.addLast(24);
        mySingleList.addLast(34);
        mySingleList.addLast(91);


        mySingleList.display();


        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(11);
        mySingleList2.addLast(21);
        mySingleList2.addLast(41);
        mySingleList2.addLast(51);


        mySingleList2.display();

        System.out.println();
       MySingleList.ListNode ret =  mergeTwoLists(mySingleList.head,mySingleList2.head);

       mySingleList2.display(ret);
//        System.out.println("翻转--------------》");
//
//        mySingleList.reverseList();
//        mySingleList.display();
    }



    public static void main1(String[] args) {
        MySingleList singleList = new MySingleList();
//        singleList.createList();
        singleList.addIndex(0,12);

        singleList.addLast(12);
        singleList.addLast(12);
        singleList.addLast(12);
        singleList.addLast(12);
        singleList.addIndex(2,12);

        singleList.display();
        System.out.println();
//        singleList.remove(34);
        System.out.println("======");
        singleList.removeAllKey(12);
        singleList.display();

        System.out.println("======");

//        System.out.println();
//        System.out.println(singleList.contains(12));
//        System.out.println(singleList.contains(100));
//        System.out.println(singleList.size());
    }
}
